package cjj;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

@WebServlet("/admin/updateMovie")
public class AdminUpdateMovieServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer adminId = (Integer) request.getSession().getAttribute("adminId");
        if (adminId == null) {
            response.sendRedirect("/admin/login");
            return;
        }

        String movieIdParam = request.getParameter("movieId");
        Integer movieId = Integer.valueOf(movieIdParam);
        String title = request.getParameter("title");
        String director = request.getParameter("director");
        // 获取其他字段

        try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/webproject_2025", "root", "root");
             PreparedStatement stmt = conn.prepareStatement("UPDATE movie SET TITLE = ?, DIRECTOR = ? WHERE MOVIE_ID = ?")) {
            stmt.setString(1, title);
            stmt.setString(2, director);
            // 设置其他字段
            stmt.setInt(3, movieId);
            stmt.executeUpdate();
            response.sendRedirect("/admin/manageMovies");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}